package com.hotel.room.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.common.vo.R;
import com.hotel.room.mapper.HotelRoomMapper;
import com.hotel.user.entity.HotelRoom;
import com.hotel.user.entity.HotelRoomCollect;
import com.hotel.room.mapper.HotelRoomCollectMapper;
import com.hotel.room.service.HotelRoomCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author trc
 * @since 2024-06-17
 */
@Service
public class HotelRoomCollectServiceImpl extends ServiceImpl<HotelRoomCollectMapper, HotelRoomCollect> implements HotelRoomCollectService {

    @Autowired
    private HotelRoomCollectMapper hotelRoomCollectMapper;

    @Autowired
    private HotelRoomMapper hotelRoomMapper;

    @Override
    public R changeRoomCollect(Integer collectStatus, Integer roomId, Integer id) {
        if (collectStatus == 1) {
            HotelRoomCollect hotelRoomCollect = hotelRoomCollectMapper.selectRoomByIdAndRoomId(roomId, id);
            hotelRoomCollectMapper.selectRoomByIdAndRoomId(roomId, id);
            if (hotelRoomCollect == null) {
                HotelRoomCollect roomCollect = new HotelRoomCollect();
                roomCollect.setRoomId(roomId);
                roomCollect.setUserId(id);
                roomCollect.setCtime(new Date());
                hotelRoomCollectMapper.insert(roomCollect);
            }
            return R.ok("已收藏");
        } else if (collectStatus == 0) {
            HotelRoomCollect hotelRoomCollect = hotelRoomCollectMapper.selectRoomByIdAndRoomId(roomId, id);
            if (hotelRoomCollect != null) {
                int result = hotelRoomCollectMapper.deleteRoomByIdAndRoomId(roomId, id);
                if (result != 0) {
                    return R.ok("取消收藏");
                }
                return R.fail("取消收藏失败");
            }
            return R.ok("取消收藏");
        }
        return R.fail("收藏出错");
    }

    @Override
    public R selectRoomCollect(Integer roomId, Integer id) {
        HotelRoomCollect hotelRoomCollect = hotelRoomCollectMapper.selectRoomByIdAndRoomId(roomId, id);
        if (hotelRoomCollect != null) {
            return R.ok(1);
        }
        return R.ok(0);
    }

    @Override
    public R showUserCollectRoom(Integer id) {

        // 先根据用户id查找到对应用户收藏的房间部分信息
        QueryWrapper<HotelRoomCollect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq("user_id",id);
        List<HotelRoomCollect> collectList = hotelRoomCollectMapper.selectList(collectQueryWrapper);
        if (collectList == null || collectList.isEmpty()) {
            return R.fail("获取失败");
        }

        // 再根据room的id查找room信息
        List<HotelRoom> collectRoom = new ArrayList<>();
        for (HotelRoomCollect collect : collectList) {
            Integer roomId = collect.getRoomId();
            QueryWrapper<HotelRoom> roomQueryWrapper = new QueryWrapper<>();
            roomQueryWrapper.eq("id", roomId);
            HotelRoom hotelRoom = hotelRoomMapper.selectOne(roomQueryWrapper);
            collectRoom.add(hotelRoom);
        }
        return R.ok(collectRoom);

    }
}
